/*
 * support3
 */
package com.pitop.errorcode.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.errorcode.entity.ErrorcodePool;
import com.pitop.errorcode.entity.ErrorcodePoolState;
import com.pitop.errorcode.service.ErrorcodePoolSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 错误码库 控制器
 *
 * @author support3
 */
@RestController
@RequestMapping("/errorcodePool")
@Slf4j
@Api(value = "错误码库控制器", description = "错误码库控制器")
public class ErrorcodePoolCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private ErrorcodePoolSV errorcodePoolSV;

    /**
     * 查询ErrorcodePool一个详情信息
     *
     * @param id
     * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ErrorcodePool一个详情信息", notes = "查询ErrorcodePool一个详情信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
        @ApiImplicitParam(name = "errorCode", value = "错误码 由人为定义并制定出来指定一定场景模块的错误编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String errorCode) {
        if (id == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (errorCode == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        ErrorcodePool errorcodePool = errorcodePoolSV.load(id, errorCode);
        log.info(JSON.toJSONString(errorcodePool));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, errorcodePool);
    }


    /**
     * 根据条件errorCode查询ErrorcodePool一个详情信息
     *
     * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ErrorcodePool一个详情信息", notes = "查询ErrorcodePool一个详情信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "errorCode", value = "错误码 由人为定义并制定出来指定一定场景模块的错误编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/errorCode/{errorCode}")
    @ResponseBody
    public BeanRet loadByErrorCode(@PathVariable String errorCode) {
        if (errorCode == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        ErrorcodePool errorcodePool = errorcodePoolSV.loadByErrorCode(errorCode);
        log.info(JSON.toJSONString(errorcodePool));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, errorcodePool);
    }


    /**
     * 查询ErrorcodePool信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ErrorcodePool信息集合", notes = "查询ErrorcodePool信息集合")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore ErrorcodePool errorcodePool, Integer curPage, Integer pageSize) {
        Page<ErrorcodePool> page = new Page<ErrorcodePool>(pageSize, curPage);
        List<ErrorcodePool> errorcodePools = errorcodePoolSV.list(errorcodePool, page.genRowStart(), page.getPageSize());
        page.setVoList(errorcodePools);
        int total = errorcodePoolSV.count(errorcodePool);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, page);
    }


    /**
     * 统计ErrorcodePool信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计ErrorcodePool信息数量", notes = "统计ErrorcodePool信息数量")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "errorCode", value = "错误码 由人为定义并制定出来指定一定场景模块的错误编码", paramType = "query"),
        @ApiImplicitParam(name = "info", value = "文本对照,默认的错误信息提示", paramType = "query"),
        @ApiImplicitParam(name = "summary", value = "错误码说明解释错误码的作用和表达意思", paramType = "query"),
        @ApiImplicitParam(name = "state", value = "状态： 启用 enable,禁用 disable", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore ErrorcodePool errorcodePool) {
        return errorcodePoolSV.count(errorcodePool);
    }


    /**
     * 创建错误码库
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建错误码库", notes = "创建错误码库")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "errorCode", value = "错误码", paramType = "query"),
        @ApiImplicitParam(name = "info", value = "文本对照,默认的错误信息提示", paramType = "query"),
        @ApiImplicitParam(name = "summary", value = "错误码说明解释错误码的作用和表达意思", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore ErrorcodePool errorcodePool) {
        if(errorcodePool == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(errorcodePool.getInfo())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isBlank(errorcodePool.getErrorCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        ErrorcodePool errorcodePoolParam = errorcodePoolSV.loadByErrorCode(errorcodePool.getErrorCode());
        if (errorcodePoolParam != null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Exists);
        }
        errorcodePoolSV.save(errorcodePool);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, errorcodePool);
    }


    /**
     * 修改错误码库
     * 1.数据合法性验证
     * ****数据非空验证
     * ****错误码是否存在
     *2.修改错误码库
     * @return BeanRet
     */
    @ApiOperation(value = "修改错误码库", notes = "修改错误码库")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "errorCode", value = "错误码 由人为定义并制定出来指定一定场景模块的错误编码", paramType = "query", required =  true),
        @ApiImplicitParam(name = "info", value = "文本对照,默认的错误信息提示", paramType = "query",required = true),
        @ApiImplicitParam(name = "summary", value = "错误码说明解释错误码的作用和表达意思", paramType = "query"),
        @ApiImplicitParam(name = "state", value = "状态(1016)： 启用 Enable,禁用 Disable", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore ErrorcodePool errorcodePool) {
        //1.数据合法性验证
        if(errorcodePool == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(errorcodePool.getErrorCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(errorcodePool.getInfo())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(errorcodePool.getState())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        //错误码是否存在
        ErrorcodePool errorcodePoolFlag = errorcodePoolSV.loadByErrorCode(errorcodePool.getErrorCode());
        if(errorcodePoolFlag == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //2.修改错误码库
        errorcodePoolSV.modify(errorcodePool);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, errorcodePool);
    }


    /**
     * 修改错误码库状态
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改错误码库状态", notes = "修改错误码库状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "errorCode", value = "错误码 由人为定义并制定出来指定一定场景模块的错误编码", paramType = "query",required = true),
            @ApiImplicitParam(name = "state", value = "状态(1016)： 启用 Enable,禁用 Disable", paramType = "query",required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String errorCode, ErrorcodePoolState state) {
        if(StringTools.isEmpty(errorCode)) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(state == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        errorcodePoolSV.updateState(errorCode, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

}
